package com.baseframe.launch.scan;

import com.baseframe.launch.utils.ClazzScanUtil;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.stream.Collectors;

public class BeanScanHandler {



    /**
     * 扫描指定注解的类，并实例化
     * @param source 源类
     * @return 源类的所有子类
     * @param <T> 子类泛型
     */
    public static <T> List<T> scanBean(Class<T> source, Class<? extends Annotation> annotSource){
        String packagePath = BeanScanHandler.class.getPackage().getName();
        String[] split = packagePath.split("\\.");
        String path = "";
        if (split.length>=1&&split.length<=3){
            path = split[0];
        }else if (split.length>3){
            path = split[0]+"."+split[1];
        }

        List<T> scan = ClazzScanUtil.scan(source, path);


        return scan
                .stream()
                .filter(e -> e.getClass().getAnnotation(annotSource) != null)
                .collect(Collectors.toList());
    }

    public static <T> List<T> scanBean(Class<T> source,Class<? extends Annotation> annotSource,String packagePath){
        List<T> scan = ClazzScanUtil.scan(source, packagePath);


        return scan
                .stream()
                .filter(e -> e.getClass().getAnnotation(annotSource) != null)
                .collect(Collectors.toList());
    }



}
